package com.qianda.qdminiapp.controller.user;


import com.qianda.qdminiapp.common.constant.CommonConstant;
import com.qianda.qdminiapp.controller.Result;
import com.qianda.qdminiapp.domain.dto.BindPhoneDto;
import com.qianda.qdminiapp.domain.model.user.QqdUser;
import com.qianda.qdminiapp.security.SecurityUser;
import com.qianda.qdminiapp.service.QqdUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cw
 * @since 2019-08-14
 */
@RestController
@RequestMapping("/qqd-user")
public class QqdUserController {

    @Autowired
    private QqdUserService qqdUserService;

    /*
     * 用户绑定手机号
     * @Description
     * @Author cw
     * @Date 10:10 2019/8/14
     * @Param [user, bindPhoneDto]
     * @return com.qianda.qdminiapp.controller.Result
     **/
    @PostMapping("/bindPhone")
    public Result bindPhone(@RequestBody BindPhoneDto bindPhoneDto, HttpServletRequest request , HttpServletResponse resopn ){
        qqdUserService.bindPhone(bindPhoneDto.getPhone(),bindPhoneDto.getOpenId());
        request.setAttribute("code", "121");
        return Result.success();
    }

    /*
     * 获取用户信息
     * @Description
     * @Author cw
     * @Date 13:26 2019/8/30
     * @Param []
     * @return com.qianda.qdminiapp.controller.Result
     **/
    @GetMapping("/getUserInfo")
    public Result getUserInfo(@AuthenticationPrincipal SecurityUser user){
        return Result.success(qqdUserService.getById(user.getId()));
    }


    /*
     * 判断用户是否设置了支付密码
     * @Description
     * @Author cw
     * @Date 13:26 2019/8/30
     * @Param []
     * @return com.qianda.qdminiapp.controller.Result
     **/
    @GetMapping("/checkPayPassword")
    public Result checkIsHavePayPassword(@AuthenticationPrincipal SecurityUser user){
        return Result.success(qqdUserService.checkHavePayPassWord(user.getId()));
    }

    /*
     * 判断用户是否为新用户
     * @Description
     * @Author cw
     * @Date 15:16 2019/9/3
     * @Param []
     * @return com.qianda.qdminiapp.controller.Result
     **/
    @GetMapping("/checkUser")
    public Result checkUserIsNew(@AuthenticationPrincipal SecurityUser user){
        QqdUser qqdUser = qqdUserService.getById(user.getId());
        if(qqdUser.getIsNewUser() == CommonConstant.IsNewUser.IS_NEW_USER){
            return Result.success(true);
        }else{
            return Result.success(false);
        }
    }

    /*
     * 保存用户信息
     * @Description
     * @Author cw
     * @Date 15:16 2019/9/3
     * @Param []
     * @return com.qianda.qdminiapp.controller.Result
     **/
    @PostMapping("/saveUserInfo")
    public Result saveUserInfo(@RequestBody QqdUser qqdUser,  @AuthenticationPrincipal SecurityUser user){
        qqdUserService.saveUserInfo(qqdUser.getNickname(),qqdUser.getAvatar(),user.getId());
        return  Result.success();
    }

}

